import React, { useState, useCallback } from 'react';
import { history } from 'umi'
import { Button, Typography, Spin } from 'antd';

import { useRequest, defaultFormatResult } from '@/hooks';
import { BottomAction } from '@/components';
import { getCommunicateSaveInfo, getCommunicateExtraInfo } from '@/pages/Recruitment/service';

import { DEPARTMENT_SUGGEST_TYPE_ENUM, NOTICE_TYPE } from '@/pages/Recruitment/dictionary';
import { sameTabProps } from '@/utils/tab';

import Edit from './Edit';

const { Text } = Typography;
export default function Communicate(props) {
    const {
        location: { query }
    } = props;

    // 判断当前状态,又是神奇的设计
    const { type, communicateCode, candidateId, action, employId } = query;

    const isSecond = type === 'hr'; // HR二次确认
    const isDepart = type === 'dept'; // 用人部门
    const isEdit = action === 'edit'; // 可编辑表单
    const [triggerRefresh, setTriggerRefresh] = useState(0);

    const reloadAfterSave = useCallback(() => {
        setTriggerRefresh(triggerRefresh + 1);
    }, [triggerRefresh]);

    // 沟通信息
    // const { data: commInfoList = [], loading: commInfoListLoading } = useRequest(() => getCommunicateInfo(employId), {
    //     refreshDeps: [employId, isEdit],
    //     ready: isEdit,
    //     ...defaultFormatResult
    // });

    // 二次沟通&用人部门信息
    const { data: commInfoExtra = {}, loading: commInfoExtraLoading } = useRequest(
        () => getCommunicateExtraInfo(communicateCode),
        {
            refreshDeps: [communicateCode, isEdit],
            ready: !isEdit,
            ...defaultFormatResult
        }
    );

    // 获取保存的沟通消息，这个请求在用人部门薪资沟通时不会发起--因为没有employId
    const { data: commInfoList = [], loading: commInfoListLoading } = useRequest(
        () => getCommunicateSaveInfo(employId),
        {
            refreshDeps: [employId, isEdit, triggerRefresh],
            ready: isEdit,
            ...defaultFormatResult
        }
    );

    const commInfo = commInfoList?.[0] ?? commInfoExtra; // 从服务端获取已保存数据，二次沟通或是暂存数据

    const loading = commInfoListLoading || commInfoExtraLoading;

    // 人事部门意见
    const departStatus = commInfo?.recruitDeptRecordVO?.suggestedStatus;
    const childProps = {
        isSecond,
        isDepart,
        isEdit,
        commInfo,
        communicateCode,
        candidateId,
        employId: employId || commInfo?.employId
    };

    const renderBottom = () => {
        if (isEdit) return null;

        if (isSecond && departStatus === DEPARTMENT_SUGGEST_TYPE_ENUM.CHANGE) {
            return (
                <BottomAction>
                    <Text disabled>再次联系候选人沟通确认录用薪资</Text>
                    <Button
                        type="primary"
                        onClick={() => {
                            history.push({
                                pathname: '/office/personnelManagement/recruitment/hire/communicate',
                                query: {
                                    employId: commInfo?.employId,
                                    communicateCode,
                                    candidateId,
                                    action: 'edit',
                                    type: 'hr'
                                },
                                ...sameTabProps
                            });
                        }}
                    >
                        薪资沟通
                    </Button>
                </BottomAction>
            );
        }

        if (isSecond && departStatus === DEPARTMENT_SUGGEST_TYPE_ENUM.OUT) {
            return (
                <BottomAction>
                    <Button
                        onClick={() => {
                            history.push({
                                pathname: '/office/personnelManagement/recruitment/candidate/notice',
                                query: {
                                    candidateId,
                                    noticeType: NOTICE_TYPE.TERMINATE
                                }
                            });
                        }}
                    >
                        通知候选人
                    </Button>
                </BottomAction>
            );
        }

        return null;
    };

    return (
        <>
            <Spin spinning={loading}>
                <Edit {...props} {...childProps} reloadAfterSave={reloadAfterSave} />
                {renderBottom()}
            </Spin>
        </>
    );
}
